class code6 {
    public int trap(int[] height) {
        int ret = 0;
        int l = 0;
        int r = height.length-1;
        int preM = 0;
        int sufM = 0;
        while(l<r) {
            preM = Math.max(preM,height[l]);
            sufM = Math.max(sufM,height[r]);
            if(preM <sufM) {
                ret += preM-height[l++];
            } else {
                ret += sufM-height[r--];
            }
        }

        return ret;
    }
}